package com.niiwoo.civet.account.service.dubbo;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.account.dao.entity.AccountPerson;
import com.niiwoo.civet.account.dto.response.AccountPersonDTO;
import com.niiwoo.civet.account.service.AccountPersonDubboService;
import com.niiwoo.civet.account.service.local.AccountPersonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 个人存管账户服务
 *
 * @author TomXue
 * @since 2018-04-23
 */
@Service(version = "1.0.0")
@Slf4j
public class AccountPersonDubboServiceImpl implements AccountPersonDubboService {

    @Autowired
    AccountPersonService accountPersonService;

    @Override
    public AccountPersonDTO queryAccountPersonByOrgIdAndUserId(String userId, String orgId) {
        AccountPersonDTO accountPersonDTO = new AccountPersonDTO();
        try {
            AccountPerson accountPerson = accountPersonService.queryByUserIdAndOrgId(userId, orgId);
            if (accountPerson == null) {
                return null;
            }
            BeanUtils.copyProperties(accountPersonDTO, accountPerson);
        } catch (Exception e) {
            log.error("根据机构ID查询机构信息,机构ID={},发生系统异常,原因={}", orgId, e);
            throw new RuntimeException(e);
        }

        return accountPersonDTO;
    }

    @Override
    public boolean isOpenAccountPersonInOrg(String userId, String orgId) {
        return queryAccountPersonByOrgIdAndUserId(userId, orgId) != null;
    }
}
